package com.coderush.admin.sys.dao;

import com.coderush.admin.sys.mapper.EmployeeMapper;
import com.coderush.admin.sys.mapper.EmployeeRoleMapper;
import com.coderush.admin.sys.model.entity.Employee;
import com.coderush.admin.sys.model.vo.EmployeeRoleVo;
import com.coderush.admin.sys.model.vo.UserRoleVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * PackageName com.stellarforce.admin.sys.dao
 *
 * @Author Eason
 * @Version v1.0
 * @ClassName LoginDao
 * @Date 2025/8/18
 * @Description TODO
 */
@Repository
public class LoginDao {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private EmployeeRoleMapper employeeRoleMapper;

    /**
     * 根据登录账号查询员工信息
     */
    public Employee getEmployeeByLoginName(String loginName) {

        return employeeMapper.getEmployeeByLoginName(loginName);
    }

    /**
     * 根据手机号查询员工信息
     */
    public Employee getEmployeeByPhone(String phoneNumber) {
        return employeeMapper.getEmployeeByPhone(phoneNumber);
    }

    /**
     * 根据员工id查询员工角色信息
     */
    public List<UserRoleVo> getUserRoleList(Long empId) {

        List<EmployeeRoleVo> employeeRoleList = employeeRoleMapper.getEmployeeRoleList(empId);
        if (CollectionUtils.isEmpty(employeeRoleList)) {
            return null;
        }
        return employeeRoleList.stream().map(employeeRole -> {
            UserRoleVo userRole = new UserRoleVo();
            userRole.setCompanyId(employeeRole.getCompanyId());
            userRole.setDeptId(employeeRole.getDeptId());
            userRole.setRoleId(employeeRole.getRoleId());
            userRole.setRoleName(employeeRole.getRoleName());
            userRole.setRoleCode(employeeRole.getRoleCode());
            userRole.setRoleSort(employeeRole.getRoleSort());
            userRole.setRoleStatus(employeeRole.getRoleStatus());
            userRole.setRoleType(employeeRole.getRoleType());
            userRole.setDataScope(employeeRole.getDataScope());
            return userRole;
        }).toList();
    }
}